/**
 * 
 */
package weibo4j.examples.util;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;

/**
 * 固定长度队列
 * @author gary
 *
 */
public class QueueUtil implements Queue{
	//队列长度
	private int limit;
	
	Queue queue = new LinkedList();
	
	public QueueUtil(int limit){
		this.limit = limit;
	}
	
	/**
	 * 入队
	 * @param e
	 */
	@Override
	public boolean offer(Object e){
		if(queue.size() >= limit){
			//如果超出长度,入队时,先出队
			queue.poll();
		}
		return queue.offer(e);
	}
	
	/**
	 * 出队
	 * @return
	 */
	@Override
	public Object poll() {
		return queue.poll();
	}
	
	/**
	 * 获取队列
	 * @return
	 */
	public Queue getQueue(){
		return queue;
	}
	
	/**
	 * 获取限制大小
	 * @return
	 */
	public int getLimit(){
		return limit;
	}

	@Override
	public boolean add(Object e) {
		return queue.add(e);
	}

	@Override
	public Object element() {
		return queue.element();
	}

	@Override
	public Object peek() {
		return queue.peek();
	}

	@Override
	public boolean isEmpty() {
		return queue.size() == 0 ? true : false;
	}

	@Override
	public int size() {
		return queue.size();
	}

	@Override
	public Object remove() {
		return queue.remove();
	}

	@Override
	public boolean addAll(Collection c) {
		return queue.addAll(c);
	}

	@Override
	public void clear() {
		queue.clear();
	}

	@Override
	public boolean contains(Object o) {
		return queue.contains(o);
	}

	@Override
	public boolean containsAll(Collection c) {
		return queue.containsAll(c);
	}

	@Override
	public Iterator iterator() {
		return queue.iterator();
	}

	@Override
	public boolean remove(Object o) {
		return queue.remove(o);
	}

	@Override
	public boolean removeAll(Collection c) {
		return queue.removeAll(c);
	}

	@Override
	public boolean retainAll(Collection c) {
		return queue.retainAll(c);
	}

	@Override
	public Object[] toArray() {
		return queue.toArray();
	}

	@Override
	public Object[] toArray(Object[] a) {
		return queue.toArray(a);
	}
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		QueueUtil u=new QueueUtil(3);
		u.offer("sds水电费");
		u.offer("1");
		u.offer("sss");

		System.out.println(u.size());
		System.out.println(u.limit);
		
		for(int i=0;i<u.size();i++){
			Object[] o=u.toArray();
			System.out.println(o[i]);
		}
		System.out.println(u.contains("sds水电费"));
		System.out.println(u.equals("sds水电费"));
	}
}
